package xyz.erupt.flow.conf;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author PP
 * @version 1.0
 * @project erupt-flow
 * @description 工作流设置
 * @date 2024/10/14 10:38:00
 */
@Configuration
public class FlowConfig {
    @Resource
    private Environment env;
    //工作流审批流程前缀，默认erupt-
    @Value("${flow.code:erupt-}")
    public String flow_code;

    //工作流启动key默认erupt
    @Value("${flow.flowKey:erupt}")
    public String eruptFlowKey;

    public String port(){
        return env.getProperty("local.server.port");
    }

    public String ip(){
        InetAddress host = null;
        try{
            host = Inet4Address.getLocalHost();
        }catch (UnknownHostException e){
            e.printStackTrace();
        }
        String ip = host.getHostAddress();//返回格式：xxx.xxx.xxx
        return ip;
    }

    public String getServerUrl(){
        // 获取HttpServletRequest
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        // 获取协议（http或https）
        String scheme = request.getScheme(); // http或https
        // 获取请求头中的 Host 值
        String host = request.getHeader("Host"); // 从请求头获取 Host

        // 构建完整的服务器地址
        String serverUrl = scheme + "://" + host;

        return serverUrl;
    }


}
